#include "MyVertexClass.h"

using namespace std;

MyVertex::MyVertex(){
    marker	=	0;
    thisName	=	"Not set";
    thisColor	=	"blue";
}

MyVertex::~MyVertex(){
	VNext.clear();
	dNextWeight.clear();    
}

void MyVertex::ConnectTo(int TheNext,double TheWeight){
    VNext.push_back(TheNext);
    dNextWeight.push_back(TheWeight);
}

void MyVertex::ShowNext(){
    std::list<int>::iterator it1;
    std::list<double>::iterator it2;
    int i=0;
    
    it1 =VNext.begin();
    it2 =dNextWeight.begin();
    while(it1!=VNext.end()){
        std::cout << "Arc" << i+1 << ":\t" << "----->\t " << *it1 << "(w=" << *it2 << ")" << std::endl;
        i++;
        it1++;
        it2++;
    }
    if(VNext.size()==0){
        cout << "Aucun arc" << endl;
    }
}


int MyVertex::SortNext(bool Permute){
	int i;
	//list<int> temp;
	//list<double> tempw;
	list<int>::iterator it;
	list<double>::iterator itw;
	
	vector<int>::iterator itindices;
	vector<double>::iterator itweight;
	
	vector<int> indices;
	vector<double> weights;
	
	//temp		=	VVertices[inode].GetNext();
	//tempw		=	VVertices[inode].GetWeight();
	
	if(VNext.size()==0){
		return(-1);
	}
	
	it		=	VNext.begin();
	itw		=	dNextWeight.begin();
	
	while(it!=VNext.end()){
		//cout << *it << endl;
		if(indices.size()==0){
                    //premier qu'il prend, il le met devant
			indices.push_back(*it);
			weights.push_back(*itw);
		}
		else{
			itindices	=	indices.begin();
			itweight	=	weights.begin();
			
			while(itindices!=indices.end()){
				//cout << "it: " << *itw << " ; " << *itweight << endl;
				if(*itw>*itweight){
					
					weights.insert(itweight,*itw);
					indices.insert(itindices,*it);
					//itindices	=	indices.end();
					break;
				}
				else{
					itindices++;
					itweight++;
					if(itindices==indices.end()){
						indices.push_back(*it);
						weights.push_back(*itw);
						//itindices	=	indices.end();
						break;
					}
				}
			}
		}
		it++;
		itw++;
	}
	
	/*
	for(i=0;i<weights.size();i++){
		cout << weights[i] << " , " << indices[i]  << endl;
		
	}
	 */
	
	if(Permute){
		i	=	0;
		it	=	VNext.begin();
		itw	=	dNextWeight.begin();
		while(VNext.size()>0){
			VNext.pop_front();
		}
		while(dNextWeight.size()>0){
			dNextWeight.pop_front();
		}
		
		while(i<indices.size()){
			VNext.push_front(indices[i]);
			dNextWeight.push_front(weights[i]);
			i++;			
		}
	}
	
	
	return(indices[indices.size()]);
}


int MyVertex::getNext(int i){
	int k=0;
	list<int>::iterator it;
	
	it = VNext.begin();
	if(i<GetNumberVertices()){
		while(k<=i){
			it++;
			k++;
		}
		return(*it);
	}
	else{
		return(-1);
	}
}

double MyVertex::getWeight(int i){
	int k=0;
	list<double>::iterator it;

	it = dNextWeight.begin();
	if(i<GetNumberVertices()){
		while(k<i){
			it++;
			k++;
		}
		return(*it);
	}
	else{
		return(-1);
	}
}

